package com.atguigu.ssyx.acl.controller;


import com.atguigu.ssyx.acl.service.AdminService;
import com.atguigu.ssyx.acl.service.RoleService;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.acl.Admin;
import com.atguigu.ssyx.vo.acl.AdminQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;

;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "用户管理")
@RestController
@RequestMapping("/admin/acl/user")
@CrossOrigin
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private RoleService roleService;

    //1.获取后台用户分页列表(带搜索)
    @ApiOperation("获取分页列表")
    @GetMapping("{page}/{limit}")
    public Result index(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "userQueryVo", value = "查询对象", required = false)
            AdminQueryVo userQueryVo) {
        Page<Admin> pageParam = new Page<>(page, limit);
        IPage<Admin> pageModel = adminService.selectPage(pageParam, userQueryVo);
        return Result.ok(pageModel);
    }

    //2.根据ID获取某个后台用户
    @ApiOperation("根据id获取某个后台用户")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        Admin user = adminService.getById(id);
        return Result.ok(user);
    }

    //3.保存一个新的后台用户
    @ApiOperation("保存一个新的后台用户")
    @PostMapping("save")
    public Result save(@RequestBody Admin admin) {
        adminService.save(admin);
        return Result.ok(null);
    }

    //4.更新一个后台用户
    @ApiOperation("更新后台用户")
    @PutMapping("update")
    public Result request(@PathVariable Admin user) {
        adminService.updateById(user);
        return Result.ok(null);
    }

    //5.删除管理用户
    @ApiOperation("根据id删除用户")
    @DeleteMapping("remove/{id}")
    public Result removeById(@PathVariable Long id) {
        adminService.removeById(id);
        return Result.ok(null);
    }

    //6.批量删除管理用户
    @ApiOperation("批量删除多个用户")
    @DeleteMapping("batchRemove")
    public Result removeByIds(@PathVariable List<Long> ids) {
        adminService.removeByIds(ids);
        return Result.ok(null);
    }

    //7.根据用户获取角色数据
    @ApiOperation("根据用户获取角色数据")
    @GetMapping("/toAssign/{adminId}")
    public Result toAssign(@PathVariable Long adminId) {
        Map<String, Object> roleMap = roleService.findRoleByUserId(adminId);
        return Result.ok(roleMap);
    }

    //8 根据用户分配角色
    @ApiOperation("根据用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestParam Long adminId, @RequestParam Long[] roleId) {
        roleService.saveUserRoleRealtionship(adminId,roleId);
        return Result.ok(null);
    }

}
